
#coding:utf-8
"""
后台管理员表


@author: author
"""

from app import db
import datetime
from app.utils.toolfunc import encrypt
from sqlalchemy.schema import Column
from sqlalchemy.sql import sqltypes

class AdminUser(db.Model):
    
    __bind_key__ = "admin"
    __tablename__ = 'flask_admin'#表名
    
    id = Column(sqltypes.Integer, primary_key = True,autoincrement=True)
    username = Column(sqltypes.String(100), index = True,unique=True)
    password = Column(sqltypes.String(200))
    user_type = Column(sqltypes.Integer,default=0)
    create_time = Column(sqltypes.DateTime, default=datetime.datetime.now)
    
    def add(self):
        if not self.id:
            db.session.add(self)
            
    def set_password(self,password):
        """设置密码
        """
        self.password = encrypt({"password":password})
        
    def check_password(self,password):
        """密码检测
        """
        return self.password == encrypt({"password":password})
    
    @property
    def is_authenticated(self):
        return True

    @property
    def is_active(self):
        return True

    @property
    def is_anonymous(self):
        return False

    def get_id(self):
        return self.id

    def __unicode__(self):
        return self.username
        
    
